package com.glsc.ngateway.shortmsg.controller;


import com.glsc.ngateway.common.api.shortmsg.bo.ShortMsgResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
public class ShortMessageExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(ShortMessageExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ShortMsgResponse methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        logger.error(e.getMessage(),e);
        return ShortMsgResponse.instanceOfFail(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage());
    }


    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public ShortMsgResponse exceptionHandler(Exception e){
        logger.error(e.getMessage(),e);
        return ShortMsgResponse.instanceOfFail(e.getMessage());
    }


}
